/**
 * Created by zhanglihe on 15/11/19.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'fineuploader',
    'calendar'
], function ($, common, ready, Collection, template, Model, dialog, fineuploader, calendar) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'order/pays',
                condition: {limit: 10, offset: 0},
                model: {
                    tag_name: 'tr',
                    url: {},
                    event: {}
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            var _this = this;
            //日期
            $('.form_datetime').calendar();
            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.order_keyword = $('input[name="order_keyword"]').val();
                _this.collection.condition.pay_keyword = $('input[name="pay_keyword"]').val();
                _this.collection.condition.pay_type = $('[name="pay_type"]').val();
                _this.collection.condition.pay_account = $('[name="pay_account"]').val();
                _this.collection.condition.pay_start_time = $('input[name="pay_start_time"]').val();
                _this.collection.condition.pay_end_time = $('input[name="pay_end_time"]').val();
                _this.collection.condition.refund_start_time = $('input[name="refund_start_time"]').val();
                _this.collection.condition.refund_end_time = $('input[name="refund_end_time"]').val();
                _this.collection.reload();
            });

            /**
             * 按状态查询
             */
            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                $('input[name="order_keyword"]').val('');
                $('input[name="pay_keyword"]').val('');
                $('[name="pay_type"]').val('all');
                $('[name="pay_account"]').val('all');
                $('input[name="pay_start_time"]').val('');
                $('input[name="pay_end_time"]').val('');
                $('input[name="refund_start_time"]').val('');
                $('input[name="refund_end_time"]').val('');
                _this.collection.condition.order_keyword = '';
                _this.collection.condition.pay_keyword = '';
                _this.collection.condition.pay_type = 'all';
                _this.collection.condition.pay_account = 'all';
                _this.collection.condition.pay_start_time = '';
                _this.collection.condition.pay_end_time = '';
                _this.collection.condition.refund_start_time = '';
                _this.collection.condition.refund_end_time = '';
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });
        }
    };

    new index();

});